﻿<?php
require('./configs/include.php');
require(C_FULL_PATH . 'modules/m_phpass/PasswordHash.php');

class c_index extends super_controller {

    public function login() {
        if (is_empty($this->post->user)) {
            throw_exception($this->gvar['m_user_empty']);
        } else if (is_empty($this->post->password)) {
            throw_exception($this->gvar['m_password_empty']);
        }

        $cod['user']['password'] = $this->post->password;
        $cod['user']['user'] = $this->post->user;
        $options['user']['lvl2'] = "one_login";

        $this->orm->connect();
        $this->orm->read_data(array("user"), $options, $cod);
        $user = $this->orm->get_objects("user");
        $this->orm->close();

        if (empty($user[0])) {
            throw_exception($this->gvar['m_incorrect_login']);
        } else {
            $this->set_user_session($user[0]);
            $this->engine->assign('type_warning', 'success');
            $this->engine->assign('msg_warning', $this->gvar['m_correct_login']);
            $this->temp_aux = 'message.tpl';
        }
    }

    public function logout() {
        $this->destroy_session();
        $this->engine->assign('type_warning', 'success');
        $this->engine->assign('msg_warning', $this->gvar['m_correct_logout']);
        $this->temp_aux = 'message.tpl';
    }

    public function display() {

        //Function consultar categoria

        $options['categoria']['lvl2'] = "all";
        $this->orm->connect();
        $this->orm->read_data(array("categoria"), $options);
        $categorias = $this->orm->get_objects("categoria");
        $this->orm->close();

        $this->engine->assign('categoria', $categorias);

        //End Function

        $where = array(array('name' => $this->gvar['n_index'], 'link' => $this->gvar['l_index']));

        $this->engine->assign('title', $this->gvar['n_global']);
        $this->engine->assign('active', $this->gvar['n_index']);
        $this->engine->assign('where', $where);
        $this->engine->display('header2.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('consultar_producto.tpl');
        $this->engine->display('footer2.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
            $this->temp_aux = 'message.tpl';
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
        }

        $this->display();
    }

}

$call = new c_index();
$call->run();
?>